const MyBaseController = require('./MyBaseController');
const fs = require('mz/fs');
const path = require('path');

class FileController extends MyBaseController {

    async upload() {
        const file = this.ctx.request.files[0];
        const name = 'chinese-medicine/' + path.basename(file.filename);
        let result;
        try {
            result = await this.ctx.oss.put(name, file.filepath);
        }
        catch (err) {
            console.log(err);
        }
        finally {
            // 需要删除临时文件
            await fs.unlink(file.filepath);
        }
        if (result) {
            this.success(result.url)
        }
    }


}

module.exports = FileController;